package org.xobo.finance.helper.base.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.malagu.panda.dictionary.service.DictionaryService;
import org.malagu.panda.importer.model.MappingRule;
import org.malagu.panda.importer.parser.CellPreParser;
import org.malagu.panda.importer.policy.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DictCellPreParser implements CellPreParser {

  @Override
  public String getName() {
    return "字典项映射";
  }

  @Override
  public void parse(Context context) {
    Object value = context.getCurrentCell().getValue();
    context.setValue(value);

    MappingRule mappingRule = context.getCurrentMappingRule();
    String cellPreParserParam = mappingRule.getCellPreParserParam();
    if (StringUtils.isEmpty(cellPreParserParam) || value == null) {
      return;
    }

    Map<String, String> dictValueMap = new HashMap<>();
    String[] codes = cellPreParserParam.split(",");
    for (String code : codes) {
      dictValueMap.putAll(dictionaryService.getDictionaryValueKeyMap(code));
    }

    String mapValue = dictValueMap.get(value);
    if (mapValue != null) {
      context.setValue(mapValue);
    } else {
      context.setValue(value);
    }

  }


  @Autowired
  private DictionaryService dictionaryService;



}
